package remoteRpc

import (
	baseLog "gitee.com/zaiqiang231/go-base-app/base_app/log"
	"gitee.com/zaiqiang231/go-base-session/app/rpc/proto"
	"google.golang.org/grpc"
	"log"
	"sync"
)

var (
	sessionRpc *RemoteSessionRpc
	onceCache  sync.Once
)

func GetSessionRpc() *RemoteSessionRpc {
	if sessionRpc == nil {
		baseLog.Errorf("sessionRpc instance is %v", sessionRpc)
	}
	return sessionRpc
}

type RemoteSessionRpc struct {
	lock                    *sync.RWMutex
	SessionRpcServiceClient proto.SessionRpcServiceClient
}

func InitSessionRpc(rpcAddr string) (*RemoteSessionRpc, error) {
	onceCache.Do(func() {
		conn, err := grpc.Dial(rpcAddr, grpc.WithInsecure())
		if err != nil {
			log.Fatalf("rpc did not connect: %v", err)
		}
		sessionRpc = &RemoteSessionRpc{
			SessionRpcServiceClient: proto.NewSessionRpcServiceClient(conn),
		}
	})
	return sessionRpc, nil
}
